import { Editor } from "slate";
import { useSlate } from "slate-react";


export default () => {
  const editor = useSlate();

  const isMarkActive = (format: string, value?: any) => {
    const marks: any = Editor.marks(editor);
    return marks?.[format] && marks?.[format] === value;
  };


  const markValue = (format: string, value?: any) => {
    const marks: any = Editor.marks(editor);
    let v =
      marks?.[format] && marks?.[format] === value ? value : marks?.[format];
    if (v && v?.indexOf("px")) {
      v = v.trim()?.split("px")?.[0];
    }
    
    return v;
  };
 


  const toggleMark = (format: string, value?: any) => {
    const isActive = isMarkActive(format, value);

    if (isActive) {
      Editor.removeMark(editor, format);
    } else {
      Editor.addMark(editor, format, value);
    }
  };



  return {
    toggleMark,
    isMarkActive,
 
    markValue,
  };
};
